var loginManager = (function () {
    var _name;
    var _password;
    var _age;

    function oparator() {
        this.init = function (user) {
            this._name = user.name;
            this._password = user.password;
            this._age = user.age;
            console.log("....init");
        }

        this.login = function () {
            $.ajax({
                url: "/mvc/user/login",
                type: "POST",
                data: {"name": this._name, "password": this._password, "age": this._age},
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    $("#regTempl").css("display", "none");
                    $("#loginTempl").css("display", "none");
                    $("#main").text("欢迎" + JSON.stringify(data));
                }
            });
        }

        this.register = function () {
            $.ajax({
                url: "/mvc/user/register",
                type: "POST",
                data: {"name": this._name, "password": this._password, "age": this._age},
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    alert(JSON.stringify(data));
                    str = JSON.stringify(data);
                    if (str == "\"register success\"") {
                        $("#regTempl").css("display", "none");
                        $("#loginTempl").css("display", "block");
                    }
                }
            });
        }
    }

    return new oparator();
})();

$(function () {
    $("#regButton").click(function (e) {
        let name = $("input[name='name']").val();
        let password = $("input[name='password']").val();
        let age = $("input[name='age']").val();
        let user = {};
        user.name = name;
        user.age = age;
        user.password = password;
        loginManager.init(user);
        loginManager.register();
        e.preventDefault();
    })

    $("#loginButton").click(function (e) {
        let name = $("input[name='name']").eq(1).val();
        let password = $("input[name='password']").eq(1).val();
        let age = $("input[name='age']").eq(1).val();
        let user = {};
        user.name = name;
        user.age = age;
        user.password = password;
        console.log(user);
        loginManager.init(user);
        loginManager.login();
        e.preventDefault();
    })
})




